package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityOptionsCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.example.myapplication.adapter.ImageListAdapter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ImageListActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;

    private ImageListAdapter mListAdapter;

    private String[] imgList = new String[]{
            "https://cdn.wanwudezhi.com/mall-portal/image/292589_MTYzOTAxODQzOTQ0MQ==287_5976x3580.jpg",
            "https://cdn.wanwudezhi.com/mall-portal/image/292589_MTYzOTAxODQzNTU0MQ==888_5000x6666.jpg",
            "https://cdn.wanwudezhi.com/mall-portal/image/292589_MTYzOTAxODQzOTc4OQ==561_6336x3808.jpg",
            "http://test/",
            "asd",
            "https://cdn.wanwudezhi.com/seller-admin/image/water_6913bde0c2b7f2eea53c95957b52b51b.jpg",
            "https://cdn.wanwudezhi.com/mall-portal/video/3387401_MTYzODM0NTg3NDYzOQ==.mp4",
            "https://cdn.wanwudezhi.com/mall-portal/image/9067144_MTYyNzI3NzE3NDY4OA==86_3024x3024.jpg",
            "https://cdn.wanwudezhi.com/mall-portal/image/38784692_MTYyOTI3MDg0ODU0NA==215.HEIC",
            "https://cdn.wanwudezhi.com/seller-admin/image/MTYyNzYyNzE4Njg3Nw",
            "https://cdn.wanwudezhi.com/seller-admin/image/MTYyNzYyNzE4Njg3Nw==440_800x800.jpg?imageView2/2/w/750/format/webp",
            "https://cdn.wanwudezhi.com/mall-portal/video/3388822_MTYyNTkzOTEyMDUwNQ==899.mp4?vframe/jpg/offset/0",
            "https://cdn.wanwudezhi.com/static/web-static/video/e152a7f950bd12154b0a75951237a32b.mov",
            "https://cdn.wanwudezhi.com/static/web-static/video/6908f8a991b0181c5c2768ad73ef0079.mp4",
            "https://cdn.wanwudezhi.com/static/web-static/image/58154727f14e6e97254a0a5af5ba864c.png",
            "https://cdn.wanwudezhi.com/mall-portal/image/38678957_MTYxOTQwNDgwMDA2NQ==359_3024x4032.jpg",
            "https://cdn.wanwudezhi.com/mall-portal/image/11915916_MTYwNDU2MzAwMjAzMg==_240x235.gif",
            "https://up.enterdesk.com/edpic_source/03/2f/ae/032fae243ea21ace589c8daad46f0418.jpg",
            "https://cdn.wanwudezhi.com/static/web-static/image/925808024cd011ed7c93ca45bf353f46_7680x4320/ceshitupianlianjie.jpeg",
            "https://cdn.wanwudezhi.com/mall-portal/image/27218187_MTYyMzU3NzE0MDQ0OA==_1987x1204.jpg",
            "https://cdn.wanwudezhi.com/mall-portal/image/27218187_MTYyMzU3NzE0MDUyMA==_1799x1377.jpg",
            "https://c-ssl.duitang.com/uploads/blog/202102/05/20210205203735_d8d06.thumb.1000_0.jpg",
            "https://cdn.wanwudezhi.com/mall-portal/image/27218187_MTYyMzU3NzUzNDMzMA==_2000x858.jpg",
            "https://c-ssl.duitang.com/uploads/blog/202105/19/20210519213245_8f960.thumb.1000_0.jpg",
            "https://c-ssl.duitang.com/uploads/blog/202105/19/20210519213244_0a1fe.thumb.1000_0.jpg",
            "https://c-ssl.duitang.com/uploads/blog/202105/19/20210519213241_7478d.thumb.1000_0.jpg",
            "https://c-ssl.duitang.com/uploads/blog/202102/18/20210218100110_b4796.thumb.1000_0.jpg",
            "https://c-ssl.duitang.com/uploads/item/201701/15/20170115212113_XMjUF.thumb.1000_0.png",
            "https://c-ssl.duitang.com/uploads/blog/202105/19/20210519215956_64c91.thumb.1000_0.jpg",
            "https://c-ssl.duitang.com/uploads/blog/202011/21/20201121201440_a375b.thumb.1000_0.jpg",
            "https://c-ssl.duitang.com/uploads/item/201611/18/20161118091152_Cu3Pn.thumb.1000_0.jpeg",
            "https://c-ssl.duitang.com/uploads/blog/202105/14/20210514140016_a7318.thumb.1000_0.jpg",
            "https://c-ssl.duitang.com/uploads/item/201704/01/20170401110939_UCJNZ.thumb.1000_0.png",
            "https://c-ssl.duitang.com/uploads/blog/202104/24/20210424092543_db67c.thumb.1000_0.jpg",
            "https://c-ssl.duitang.com/uploads/blog/202102/22/20210222092549_41c86.thumb.1000_0.jpg",
            "https://c-ssl.duitang.com/uploads/blog/202105/16/20210516160700_5e011.thumb.1000_0.jpg",
            "https://c-ssl.duitang.com/uploads/blog/202105/03/20210503211628_62204.thumb.1000_0.jpg",
            "https://c-ssl.duitang.com/uploads/blog/202010/25/20201025215232_d4c54.thumb.1000_0.jpg",
            "https://c-ssl.duitang.com/uploads/item/202003/06/20200306195948_yjtmf.thumb.1000_0.jpg",
            "https://c-ssl.duitang.com/uploads/blog/202105/15/20210515154349_42092.thumb.1000_0.jpeg",
            "https://c-ssl.duitang.com/uploads/blog/202105/15/20210515133359_e2146.thumb.1000_0.jpg",
            "https://c-ssl.duitang.com/uploads/blog/202105/14/20210514153304_6e9e2.thumb.1000_0.jpg",
            "https://upload-images.jianshu.io/upload_images/5196125-6accb784544121d4.jpeg",
            "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fwww.ys137.com%2Fuploads%2Fcj_" +
                    "img%2F20160104%2F0104mz1lavfh45b.jpg&refer=http%3A%2F%2Fwww.ys137.com&app=2002&size=f9999," +
                    "10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1625915043&t=024e00ddd35505b1bd3add1981b8a80b",
            "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=157778531,654879109&fm=26&gp=0.jpg"
    };
    List<String> list = new ArrayList<>(Arrays.asList(imgList));

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        mRecyclerView = findViewById(R.id.recycler_view);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
        mRecyclerView.setLayoutManager(gridLayoutManager);

        mListAdapter = new ImageListAdapter();
        mListAdapter.addChildClickViewIds(R.id.iv_image);
        mListAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                if (view.getId() == R.id.iv_image) {
                    goDetail(view, position);
                }
            }
        });

        mRecyclerView.setAdapter(mListAdapter);


        initData();
    }

    private void goDetail(View view, int position) {
        Intent intent = new Intent(this, ImageDetailActivity.class);
        ActivityOptionsCompat optionsCompat =
                ActivityOptionsCompat.makeSceneTransitionAnimation(this, view, list.get(position));

        DataConfig dataConfig = new DataConfig();
        dataConfig.setList(list);
        dataConfig.setCurrentPosition(position);
        intent.putExtra(ImageDetailActivity.EXTRA_DATA, dataConfig);
        startActivity(intent, optionsCompat.toBundle());
    }

    private void initData() {
        mListAdapter.setNewInstance(list);
    }
}